﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace MapEditor
{
    // copy in: http://stackoverflow.com/questions/6059894/how-draw-rectangle-in-wpf
    public class ROM
    {
        public bool _mouseDown = false;
        public Rectangle _current;
        public Point _initialPoint;
        public Canvas canvas;

        public ROM(Canvas cv)
        {
            canvas = cv;
        }

        public void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            _mouseDown = (e.ButtonState == MouseButtonState.Pressed)
                                    && (e.ChangedButton == MouseButton.Left);
            if (!_mouseDown)
                return;

            _current = new Rectangle();
            _initialPoint = e.MouseDevice.GetPosition(canvas);
            _current.Stroke = new SolidColorBrush(Colors.Blue);
            _current.StrokeThickness = 3;
            canvas.Children.Add(_current);
        }

        public void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (!_mouseDown)
                return;

            Point position = e.MouseDevice.GetPosition(canvas);
            _current.SetValue(Canvas.LeftProperty,
                                             Math.Min(position.X, _initialPoint.X));
            _current.SetValue(Canvas.TopProperty,
                                             Math.Min(position.Y, _initialPoint.Y));
            _current.Width = Math.Abs(position.X - _initialPoint.X);
            _current.Height = Math.Abs(position.Y - _initialPoint.Y);
        }

        public void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
                _mouseDown = false;
        }
    }
}
